<template>
  <div>
    <div class="pageHeader">
      <el-form :model="filter" inline>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="filter.courseName" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="filter.status">
            <el-option
              v-for="{ name, val } of statusOptions"
              :key="val"
              :label="name"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="doFilter">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="$router.push({ name: 'course-create' })"
        ><i class="el-icon-plus"></i> 新建课程</el-button
      >
    </div>
    <el-table :data="items">
      <el-table-column label="ID" prop="id" width="50" />
      <el-table-column label="课程名称" prop="courseName" />
      <el-table-column label="价格" prop="price" />
      <el-table-column label="排序" prop="sortNum" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="changeStatus(scope.row)"
            :disabled="scope.row.disabled"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="120">
        <template slot-scope="scope">
          <el-button
            @click="
              $router.push({
                name: 'course-edit',
                params: { courseId: scope.row.id },
              })
            "
            size="small"
            >编辑</el-button
          >
          <el-button
            size="small"
            @click="
              $router.push({
                name: 'course-section',
                params: { courseId: scope.row.id },
              })
            "
            >内容管理</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.cur"
      :page-sizes="page.sizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
  </div>
</template>
<script lang="ts">
import { getQueryCourses, changeState } from '@/services/course'
import { ID } from '@/types'
import Vue from 'vue'
export default Vue.extend({
  name: 'CourseIndex',
  data () {
    return {
      items: [],
      filter: {
        courseName: '',
        status: ''
      },
      statusOptions: [
        { name: '全部', val: '' },
        { name: '上架', val: 1 },
        { name: '下架', val: 0 }
      ],
      page: {
        sizes: [20, 5, 10],
        size: 20,
        cur: 1,
        total: 0
      }
    }
  },
  created () {
    this.loadItems()
  },
  methods: {
    async loadItems () {
      const { data } = await getQueryCourses({
        currentPage: this.page.cur,
        pageSize: this.page.size,
        courseName: this.filter.courseName,
        status: this.filter.status
      })
      const items = data.data.records
      items.forEach((item: object & { disabled: boolean }) => {
        item.disabled = false
      })
      this.items = items
      this.page.total = data.data.total
    },
    async changeStatus (item: { id: ID; status: number | string; disabled: boolean }) {
      if (item.disabled) return
      item.disabled = true
      try {
        await changeState({ courseId: item.id, status: item.status })
        this.$message.success(item.status === 1 ? '已上架' : '已下架')
      } catch (error) {

      } finally {
        item.disabled = false
      }
    },
    doFilter () {
      this.handleCurrentChange(1)
    },
    handleSizeChange (size: number) {
      this.page.size = size
      this.loadItems()
    },
    handleCurrentChange (cur: number) {
      this.page.cur = cur
      this.loadItems()
    }
  }
})
</script>
<style lang="scss" scoped>
.pageHeader {
  display: flex;
  flex: row none none;
  justify-content: space-between;
  align-items: flex-start;
}
</style>
